package com.busines.MQ;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitConfig {

	@Value(value="${rmq.host}")
	private String host;  
	
	@Value(value="${rmq.port}")
	private int port;  
	
	@Value(value="${rmq.username}")
	private String username;  
	
	@Value(value="${rmq.password}")
	private String password;
	
	@Value(value="${rmq.vhost}")
	private String vhost;
	
	@Bean  
	public ConnectionFactory connectionFactory() {  
	        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);  
	        connectionFactory.setUsername(username);  
	        connectionFactory.setPassword(password);  
	        connectionFactory.setPort(port); 
	        connectionFactory.setVirtualHost(vhost);
	        return connectionFactory;
	}
	@Bean  
    public RabbitTemplate rabbitTemplate() {  
        RabbitTemplate template = new RabbitTemplate(connectionFactory());  
        return template;
	}
	
	@Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        FanoutExchange fanoutExchange=new FanoutExchange("fanoutExchange1"); 
        rabbitAdmin.declareExchange(fanoutExchange);
        Queue queue1 = new Queue("fanout.A" , true);
        Queue queue2 = new Queue("fanout.B" , true);
        Queue queue3 = new Queue("fanout.C" , true);
        Queue queue4 = new Queue("fanout.D" , true);
        rabbitAdmin.declareQueue(queue1);
        rabbitAdmin.declareQueue(queue2);
        rabbitAdmin.declareQueue(queue3);
        rabbitAdmin.declareQueue(queue4);
        
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue1).to(fanoutExchange));
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue2).to(fanoutExchange));
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue3).to(fanoutExchange));
        rabbitAdmin.declareBinding(BindingBuilder.bind(queue4).to(fanoutExchange));
        return rabbitAdmin;
	}
	  @Bean("pointTaskContainerFactory")  
	  public SimpleRabbitListenerContainerFactory pointTaskContainerFactory() {  
	      SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();  
	      factory.setConnectionFactory(connectionFactory());
	      factory.setConcurrentConsumers(1); 
	      factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
	      return factory;  
	  } 
	
}